package com.zznj.xzjc.activity;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.zznj.xzjc.R;
import com.zznj.xzjc.adapter.AreaAdapter;
import com.zznj.xzjc.adapter.AreaAdapter1;
import com.zznj.xzjc.base.BaseActivity;
import com.zznj.xzjc.net.SharedHelper;
import com.zznj.xzjc.net.entity.AreaBean;
import com.zznj.xzjc.net.entity.BaseBean;
import com.zznj.xzjc.net.entity.LoginBean;
import com.zznj.xzjc.net.rxjava.HttpMethods;
import com.zznj.xzjc.net.subscribers.ProgressSubscriber;
import com.zznj.xzjc.net.subscribers.SubscriberOnNextListener;
import com.zznj.xzjc.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.FormBody;


/**
 * Created by JY on 2017/11/15.
 */

public class SelectAreaActivity extends BaseActivity {
    @BindView(R.id.rec_main)
    RecyclerView recMain;

    @BindView(R.id.selectTv)
    TextView selectTv;
    @BindView(R.id.sureBtn)
    TextView sureBtn;
    @BindView(R.id.resetBtn)
    TextView resetBtn;

    @Override
    protected int getLayoutId() {
        return R.layout.gyss_activity_select_area_layout;
    }


    private List<AreaBean> mCityList;
    private List<AreaBean> mAreaList;
    private List<AreaBean> mList;
    private AreaAdapter1 mAdapter;
    private AreaBean area;
    private AreaAdapter areaAdapter;

    private String areaCode;
    private String selfLevel;
    private String districtLevel;

    private int fromActivity; //0--默认；1--本级

    @Override
    public void initView() {
        setTitle("区划选择");
        user = new Gson().fromJson(SharedHelper.readValueByKeyForString(mContext, "Member"), LoginBean.class);
        jurisDiction = getIntent().getExtras().getString("searchCode");;
        areaCode = getIntent().getExtras().getString("areaCode");;
        selfLevel = getIntent().getExtras().getString("selfLevel");
        districtLevel = getIntent().getExtras().getString("districtLevel");

        fromActivity = getIntent().getExtras().getInt("fromActivity");

        init();
    }


    private void init() {
        mCityList = new ArrayList<>();
        mAreaList = new ArrayList<>();
        mList = new ArrayList<>();
        final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        //防止Item切换
        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        recMain.setLayoutManager(layoutManager);

        mAdapter = new AreaAdapter1(R.layout.gyss_item_area, mList);
        recMain.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                AreaBean bean = mList.get(position);
                if(bean.getName().equals("返回")){
                    areaCode = bean.getCode();
                    jurisDiction = bean.getCode();
                    districtLevel = bean.getDistrictLevel();
                    selfLevel = bean.getSelfLevel();

                    getArea();
                }else {
                    area = bean;
                    setSelectTv();
                    if ("2".equals(bean.getDistrictLevel())) {
                        if ("1".equals(districtLevel)) {
                            districtLevel = area.getDistrictLevel();
                            selfLevel = area.getSelfLevel();
                            areaCode = area.getCode();
                            jurisDiction = area.getCode();
                            getArea();
                        }else{
                            mAdapter.setSelectPosition(position);
                        }
                    }else{
                        mAdapter.setSelectPosition(position);
                    }
                }
            }
        });
    }

    private LoginBean user;
    private String jurisDiction;


    @Override
    public void initData() {
        if(StringUtils.isEmpty(jurisDiction)) {

        }
        getArea();
    }

    private void getArea() {
        SubscriberOnNextListener<BaseBean<List<AreaBean>>> onNextListener = baseBean -> {
            mList.clear();

            mList.addAll(baseBean.getResult());
            int selectPosition = -1;
            for(int i=0;i<mList.size();i++) {
                if (mList.get(i).getSelfLevel().equals(selfLevel) && mList.get(i).getCode().equals(areaCode)) {
                    selectPosition = i;
                    break;
                }
            }
            mAdapter.setSelectPosition(selectPosition);
            area = mList.get(selectPosition);
            setSelectTv();
        };
        FormBody requestBody = new FormBody.Builder()
                .add("jurisDiction", jurisDiction)
                .build();
        HttpMethods.getInstance().getDistrict(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }

    private void setSelectTv(){

    }

    @OnClick({R.id.resetBtn,R.id.sureBtn})
    public void onViewClicked(View view) {
        if(view.getId() == R.id.resetBtn){
            areaCode = null;
            jurisDiction = null;
            districtLevel = null;
            selfLevel = null;
            mList.clear();
            initData();
        }else if(view.getId() == R.id.sureBtn){
            if(area == null){
              finish();
            }
            Intent intent = new Intent();
            intent.putExtra("areaCode",area.getCode());
            intent.putExtra("areaName",area.getName());
            intent.putExtra("upCode",area.getUpCode());
            intent.putExtra("selfLevel",area.getSelfLevel());
            intent.putExtra("districtLevel",area.getDistrictLevel());
            setResult(RESULT_OK,intent);
            finish();
        }
    }
}
